1 import java.io.*;
2 import java.net.*;
3
4 class HTTPDaemon {
5
6 int port = 80;
7
8 ServerSocket serverSocket;
9 boolean useNames = true;
10
11 public HTTPDaemon(int port) {
12 this.port = port;
13
14 try {
15 serverSocket = new ServerSocket(this.port);
16 } catch (Exception e) {
17 System.out.println("Socket Exception. message=" + e);
18 }
19 }
20
21 public void setUseHostName(boolean set) {
22 useNames = set;
23 }
24
25 public String url() {
26 String url;
27
28 if (serverSocket != null) {
29 try {
30 if (useNames) {
31 url = "http://" + InetAddress.getLocalHost().getHostName() + port;
32 } else {
33 url = "http://" + InetAddress.getLocalHost().getHostAddress() + port;
34 }
35 } catch (UnknownHostException e) {
36 }
37 return url;
38 }
39
40 return null;
41 }
42
43 public Connection accept() {
44 try {
45 Socket s = serverSocket.accept();
46
47 return new Connection(s);
48 } catch (Exception e) {
49 System.out.println("Accept Exception: " + e);
50 }
51 return null;
52 }
53 }
54
55
56
57
58
59
60
|